package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class DishServiceImpl implements DishService {

    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;

    @Transactional
    @Override
    public boolean save(DishDto dishDto) {
        //完善数据对象
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);

        //条用数据层添加菜品 获取添加后菜品添加主键id
        dishMapper.insert(dishDto);
        //遍历口味记录列表完善数据
        List<DishFlavor> dishFlavorList = dishDto.getFlavors().stream().map((dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            return dishFlavor;
        })).collect(Collectors.toList());

        dishFlavorMapper.save(dishFlavorList);

        return true;



    }

    @Override
    public Page<DishDto> findPage2(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<DishDto> dishDtoList = dishMapper.findAll2(name);
        PageInfo<DishDto> pageInfo = new PageInfo<>(dishDtoList);

        Page<DishDto> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;
    }

    @Override
    public DishDto findById(Long id) {
        return dishMapper.findById(id);
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        return dishMapper.findByCategoryId(categoryId);
    }
   //根据类别id看状态
    @Override
    public List<DishDto> findByCategoryId(Long categoryId, Integer status) {

        List<Dish> dishList = dishMapper.findByCategoryIdAndStatus(categoryId, status);
        List<DishDto> disDtoList = dishList.stream().map((dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);

            List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(dishDto.getId());
            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        })).collect(Collectors.toList());

        return disDtoList;
    }



}
